package hibernate.util;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.ttl.bc.ap.bean.BCAP;

public class TestHibernate {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		Session session = null;

		try {
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			System.out.println("open ok !!");
			
			session.beginTransaction();

			String hql = "FROM BcAP ap ";
			Query q = session.createQuery(hql);
			System.out.println("######### -> createQuery ");
			List result = q.list();
			System.out.println("######### -> q.list size= "+result.size());
			session.getTransaction().commit();			
			
			System.out.println("close ok !!");

			@SuppressWarnings("unused")
			BCAP a1 ;//= (BcAP)result.get(1);

			if (result.size()>0) {
				for (int i=0; i<result.size(); i++) {
					BCAP a = (BCAP)result.get(i);
					System.out.print("  "+a.getCode());
					System.out.print("  "+a.getName1());
					System.out.println("  "+a.getName2());					
				}
			
			}
			
		}catch(Exception e) {
			System.out.println(e.toString());
		}finally {
			session.close();
		}
	}
}
